package com.bbva.kyfb.kyfbjpatlbh.dominiotlbh;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TtlsbppaDataOnDemand.
 */
@Configurable
@Component
@RooDataOnDemand(entity = Ttlsbppa.class)
public class TtlsbppaDataOnDemand {

	/** The rnd. */
	private final Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlsbppa> data;

	/**
	 * Gets the new transient ttlsbppa.
	 *
	 * @param index the index
	 * @return the new transient ttlsbppa
	 */
	public Ttlsbppa getNewTransientTtlsbppa(final int index) {
		final Ttlsbppa obj = new Ttlsbppa();
		setEmbeddedIdClass(obj, index);
		setDesUrlaud(obj, index);
		setDesUrldet(obj, index);
		setDesUrleje(obj, index);
		setDesObjf(obj, index);
		setXsnFirmable(obj, index);
		setXsnMsgavs(obj, index);
		setCodMsgavs(obj, index);
		setXsnMsgacp(obj, index);
		setCodMsgacp(obj, index);
		setXsnFdsenv(obj, index);
		setXsnFdsrec(obj, index);
		setXsnTrahis(obj, index);
		setDesTrahis(obj, index);
		return obj;
	}


	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(final Ttlsbppa obj, final int index) {
		TtlsbppaPK embeddedIdClass;
		if(String.valueOf(index).length() > 1){
			embeddedIdClass = new TtlsbppaPK("co_" + String.valueOf(index).substring(0,1), "c" +  String.valueOf(index).substring(0,1), "c_" +  String.valueOf(index).substring(0,1));
		}else{
			embeddedIdClass = new TtlsbppaPK("co_" + index, "c" +  index, "c_" +  index);
		}
		obj.setId(embeddedIdClass);
	}

	/**
	 * Sets the des urlaud.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesUrlaud(final Ttlsbppa obj, final int index) {
		String desUrlaud = "desUrlaud_" + index;
		if (desUrlaud.length() > 254) {
			desUrlaud = desUrlaud.substring(0, 254);
		}
		obj.setDesUrlaud(desUrlaud);
	}

	/**
	 * Sets the des urldet.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesUrldet(final Ttlsbppa obj, final int index) {
		String desUrldet = "desUrldet_" + index;
		if (desUrldet.length() > 254) {
			desUrldet = desUrldet.substring(0, 254);
		}
		obj.setDesUrldet(desUrldet);
	}

	/**
	 * Sets the des urleje.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesUrleje(final Ttlsbppa obj, final int index) {
		String desUrleje = "desUrleje_" + index;
		if (desUrleje.length() > 254) {
			desUrleje = desUrleje.substring(0, 254);
		}
		obj.setDesUrleje(desUrleje);
	}

	/**
	 * Sets the des objf.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesObjf(final Ttlsbppa obj, final int index) {
		String desObjf = "desObjf_" + index;
		if (desObjf.length() > 254) {
			desObjf = desObjf.substring(0, 254);
		}
		obj.setDesObjf(desObjf);
	}

	/**
	 * Sets the xsn firmable.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnFirmable(final Ttlsbppa obj, final int index) {
		final Character xsnFirmable = new Character('N');
		obj.setXsnFirmable(xsnFirmable);
	}

	/**
	 * Gets the specific ttlsbppa.
	 *
	 * @param index the index
	 * @return the specific ttlsbppa
	 */
	public Ttlsbppa getSpecificTtlsbppa(int index) {
		init();
		if (index < 0) {
			index = 0;
		}
		if (index > data.size() - 1) {
			index = data.size() - 1;
		}
		final Ttlsbppa obj = data.get(index);
		return Ttlsbppa.findTtlsbppa(obj.getId());
	}

	/**
	 * Gets the random ttlsbppa.
	 *
	 * @return the random ttlsbppa
	 */
	public Ttlsbppa getRandomTtlsbppa() {
		init();
		final Ttlsbppa obj = data.get(rnd.nextInt(data.size()));
		return Ttlsbppa.findTtlsbppa(obj.getId());
	}

	/**
	 * Sets the des trahis.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	private void setDesTrahis(Ttlsbppa obj, int index) {
		String desTrahis = "desTrahis_" + index;
		if (desTrahis.length() > 100) {
			desTrahis = desTrahis.substring(0, 100);
		}
		obj.setDesTrahis(desTrahis);
	}

	/**
	 * Sets the xsn trahis.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	private void setXsnTrahis(Ttlsbppa obj, int index) {
		final Character xsnTrahis = new Character('N');
		obj.setXsnTrahis(xsnTrahis);
	}

	/**
	 * Sets the xsn fdsrec.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	private void setXsnFdsrec(Ttlsbppa obj, int index) {
		final Character xsnFdsrec = new Character('N');
		obj.setXsnFdsrec(xsnFdsrec);
	}

	/**
	 * Sets the xsn fdsenv.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	private void setXsnFdsenv(Ttlsbppa obj, int index) {
		final Character xsnFdsenv = new Character('N');
		obj.setXsnFdsenv(xsnFdsenv);
	}

	/**
	 * Sets the cod msgacp.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	private void setCodMsgacp(final Ttlsbppa obj, final int index) {
		String codMsgacp = "c_" + index;
		if (codMsgacp.length() > 3) {
			codMsgacp = codMsgacp.substring(0, 3);
		}
		obj.setCodMsgacp(codMsgacp);

	}

	/**
	 * Sets the xsn msgacp.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	private void setXsnMsgacp(final Ttlsbppa obj, final int index) {
		final Character xsnMsgacp = new Character('N');
		obj.setXsnMsgacp(xsnMsgacp);
	}

	/**
	 * Sets the cod msgavs.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	private void setCodMsgavs(final Ttlsbppa obj, final int index) {
		String codMsgavs = "c_" + index;
		if (codMsgavs.length() > 3) {
			codMsgavs = codMsgavs.substring(0, 3);
		}
		obj.setCodMsgavs(codMsgavs);

	}

	private void setXsnMsgavs(final Ttlsbppa obj, final int index) {
		final Character xsnMsgavs = new Character('N');
		obj.setXsnMsgavs(xsnMsgavs);
	}

	/**
	 * Modify ttlsbppa.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlsbppa(final Ttlsbppa obj) {
		return false;
	}

	/**
	 * Inits the.
	 */
	public void init() {
		data = Ttlsbppa.findTtlsbppaEntries(0, 10);
		if (data == null) {
			throw new IllegalStateException("Find entries implementation for 'Ttlsbppa' illegally returned null");
		}
		if (!data.isEmpty()) {
			return;
		}

		data = new ArrayList<com.bbva.kyfb.kyfbjpatlbh.dominiotlbh.Ttlsbppa>();
		for (int i = 0; i < 10; i++) {
			final Ttlsbppa obj = getNewTransientTtlsbppa(i);
			try {
				obj.persist();
			} catch (final ConstraintViolationException e) {
				final StringBuilder msg = new StringBuilder();
				for (final ConstraintViolation<?> cv : e.getConstraintViolations()) {
					msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
				}
				throw new RuntimeException(msg.toString(), e);
			}
			obj.flush();
			data.add(obj);
		}
	}
}
